<template>
  <div class="article-container">
    <el-card class="filter-card">
      <div slot="header" class="clearfix">
        <!-- 导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>内容管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div  class="item">
        <!-- 数据筛选 -->
        <el-form ref="form"  label-width="50px"  size="mini">
          <el-form-item label="状态">
            <el-radio-group v-model="status">
            <!--
               el-radio默认把label作文文本和选中之后的value值
            -->
              <el-radio :label="null" >全部</el-radio>
              <el-radio :label="0">草稿</el-radio>
              <el-radio :label="1">待审核</el-radio>
              <el-radio :label="2">审核通过</el-radio>
              <el-radio :label="3">审核失败</el-radio>
              <el-radio :label="4">已删除</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="频道">
            <el-select v-model="channelsId" placeholder="请选择频道">
              <el-option label="全部" :value="null"></el-option>
              <el-option v-for="(item,index) in ChannelsList" :key="index" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="日期">
            <el-date-picker
              v-model="date"
              type="datetimerange"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['12:00:00']"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :disabled="loading"  @click="getArticleList(1)">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="filter-content">
      <div slot="header" class="clearfix">
          根据筛选条件共查询{{total_count}}
      </div>
      <!-- 数据列表 -->
      <!--
          1.把需要展示的数组列表数据给table组件的data属性
            注意不用v-for遍历  会自己遍历数据
          2.设计表格列 el-table-column  width： 可以设定表格列的宽度 label：可以设定列的标题  prop：数据
          3.表格列默认只能渲染普通文本。如果需要展示其他内容例如 按钮、图片  那么就需要模板了
          4.
       -->
      <el-table
        :data="listData"
        stripe
        style="width: 100%"
        class="list-table"
        size="small"
        v-loading="loading"
        >
        <el-table-column
          label="封面"
        >
          <template slot-scope="scope">
            <el-image
               class="cover-img"
               :src="scope.row.cover.images[0]"
               fit="cover"
              >
              <div slot="placeholder" class="image-slot">
                加载中<span class="dot">...</span>
              </div>
            </el-image>
            <!--<img v-if="scope.row.cover.images[0]" :src="scope.row.cover.images[0]" class="cover-img" alt="">
             <img v-else src="~@/assets/zw.jpg" class="cover-img" alt="">-->
          </template>
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          >
        </el-table-column>
        <el-table-column
          label="状态"
          >
          <!-- 如果需要在自定义模板获取当前遍历项数据 那么就在tempalte 声明slot-scope="scope" -->
          <template slot-scope="scope">
            <!-- <el-tag v-if="scope.row.status===0">草稿</el-tag>
            <el-tag type="warning" v-else-if="scope.row.status===1">待审核</el-tag>
            <el-tag type="success" v-else-if="scope.row.status===2">审核通过</el-tag>
            <el-tag type="info"  v-else-if="scope.row.status===3">审核失败</el-tag>
            <el-tag type="danger"  v-else-if="scope.row.status===4">已删除</el-tag> -->
             <el-tag :type="articleStatus[scope.row.status].type">{{articleStatus[scope.row.status].text}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="pubdate"
          label="发布时间"
         >
        </el-table-column>
        <el-table-column
          label="操作">
            <template slot-scope="scope">
              <el-button type="primary" icon="el-icon-edit" circle @click="$router.push('/publish?id='+scope.row.id.toString())"></el-button>
              <el-button type="danger" icon="el-icon-delete" @click="onDelete(scope.row.id)" circle></el-button>
            </template>
        </el-table-column>
      </el-table>
      <!-- 数据分页 -->
      <el-pagination
        layout="prev, pager, next"
        :disabled="loading"
        background
        :total="total_count"
        :page-size="pageSize"
        :current-page.sync="page"
        @current-change="onCurrentChange"
        >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import { getArticles, getChannels, deleteChannels } from '@/api/article.js'
export default {
  name: 'articleIndex',
  components: {},
  props: {},
  data () {
    return {
      date: null || [], // 筛选日期
      listData: [], // 文章数据列表
      articleStatus: [
        { text: '草稿', status: 0, type: '' },
        { text: '待审核', status: 1, type: 'warning' },
        { text: '审核通过', status: 2, type: 'success' },
        { text: '审核失败', status: 3, type: 'info' },
        { text: '已删除', status: 4, type: 'danger' }
      ],
      total_count: 0, // 总数据数
      pageSize: 10, // 每页大小 默认每页10条数据
      status: null, // 查询文章的状态  不传表示全部
      ChannelsList: [], // 频道列表
      channelsId: null, // channel_id 查询文章频道id
      loading: true, // 加载
      page: 1 // 当前页码  当前位于哪个页码  删除操作会用到
    }
  },
  computed: {},
  watch: {},
  created () {
    this.getArticleList()
    this.getChannels()
  },
  mounted () {},
  methods: {
    // 获取文章列表
    getArticleList (page = 1) {
      this.loading = true
      getArticles({
        page: page,
        per_page: this.pageSize,
        status: this.status, // axios不会发送数据为null、undefined之类的数据
        channel_id: this.channelsId,
        begin_pubdate: this.date ? this.date[0] : null, // 开始日期
        end_pubdate: this.date ? this.date[1] : null// 结束日期
      }).then(res => {
        const { results, total_count: totalCount } = res.data.data
        this.listData = results
        this.total_count = totalCount
        this.loading = false // 关闭加载loading
        // console.log(res)
      })
    },
    // 分页
    onCurrentChange (page) {
      this.getArticleList(page)
    },
    // 获取频道
    getChannels () {
      getChannels().then(res => {
        const { channels } = res.data.data
        // console.log(channels)
        this.ChannelsList = channels
      })
    },
    // 删除
    onDelete (articleId) {
      // 1.找到数据接口
      // 2.封装请求方法
      // 3.删除请求调用
      // 4.处理响应结果
      // console.log(articleId)
      // console.log(articleId.toString())
      this.$confirm('确认删除吗?', '删除提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteChannels(articleId.toString()).then(res => {
          // console.log(res)
          this.getArticleList(this.page) // 删除成功后  更新当前页码数据列表
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang="less">
 .filter-card{
    margin-top:10px;
 }
 .filter-content{
    margin-top:20px;
 }
 .list-table{
   margin-bottom: 20px;
 }
 .cover-img{
    width:100px;
    height:auto;
    object-fit: cover;
 }
</style>
